package iscript.gameFramework.NetService.data 
{
	import flash.utils.ByteArray;
	import flash.utils.Endian;
	import iscript.coreFramework.debug.ArthropodDebug;
	import iscript.gameFramework.NetService.util.BigInt;

	/**
	 * @copy parse在加入到BufferList中使用
	 */
	
	public class NetMessageBase 
	{
		
		public function NetMessageBase() {}
		
		static public const Empty:String = "NetMessageEmpry";
		
		public var MainId:int;
		public var ChildId:int;
		private var _data:ByteArray;
		
		public function get data():ByteArray { return _data; }
		
		public function parse(msg:ByteArray):void {
			msg.position = 0;
			MainId =  toShort(msg);
			ChildId = toShort(msg);
		}
			
		/**
		 * N位 字符串
		 * @param	msg
		 * @return
		 */
		protected  function toStr(msg:ByteArray,byteType:String="utf-8"):String
		{
            var stringLenth:int = msg.readShort();
			var result:String = msg.readMultiByte(stringLenth, byteType);
			return  result;
		}
		
		/**
		 * 1位   一个无符号Byte值
		 * @param	msg
		 */
		protected  function toByte(msg:ByteArray):int
		{	
			return msg.readUnsignedByte();
		}
		
		/**
		 * 4位  有符号32-bit整数值
		 * @param	msg
		 */
		protected  function toInt(msg:ByteArray):int
		{	
			return msg.readInt();
		}
		
		/**
		 * 浮点数
		 * @param	msg
		 * @param	$decimal
		 * @return
		 */
		protected  function toFloat(msg:ByteArray,$decimal:int = 2):Number
		{
			var result:Number = msg.readFloat();
			return Number(result.toFixed($decimal));
		}
		
		/**
		 * 2位 有符号的
		 * @param	msg
		 */
		protected  function toWord(msg:ByteArray):int
		{
             return  msg.readShort();
		}
		
		/**
		 * 4位  无符号32-bit整数
		 * @param	msg
		 */
		protected  function toDword(msg:ByteArray):int
		{
			return msg.readUnsignedInt();
		}
		
		/**
		 * 2位 有符号16-bit整数值.  
		 * @param	msg
		 */
		protected  function toShort(msg:ByteArray):int
		{
			return  msg.readShort();
		}	
		
		/**
		 * 1位  一个Boolean值
		 * @param	msg
		 */
		protected  function toBoolean(msg:ByteArray):Boolean
		{
			return msg.readBoolean();
		}		
		
		/**
		 * 8位  一个64-bit值
		 * @param	msg
		 * @return
		 */
	    protected  function toInt64(msg:ByteArray):BigInt
		{
			var result:BigInt = new BigInt();
			result.light = msg.readInt();
			result.high = msg.readInt();
			return result;
		}
		
		protected  function toInt64Number(msg:ByteArray):Number
		{
			var result:BigInt = new BigInt();
			result.light = msg.readInt();
			result.high = msg.readInt();
			return result.high * Math.pow(2,32) + result.light;	 
		}
		
		
	}

}